const mutations = {

    // 保存购物车数据
    setCartListFn(state, list) {


        state.cartList = list
        localStorage.setItem('cartList', JSON.stringify(list))
    },

    // 用于对比
    setOldList(state,list){
        state.oldList = list
        // localStorage.setItem('cartList', JSON.stringify(list)) 
    },

    // 修改商品勾选状态
    setGoodStatus(state,obj){
        if(state.cartList.length > 0){
            state.cartList.some((item,index)=>{
                if(item.cartId == obj.id){
                    item.status = obj.status
                    console.log(item.state)
                    return
                }
            })

            localStorage.setItem('cartList', JSON.stringify(state.cartList))
        }
    },

    // 全选
    setGoodAllStatus(state,status){
        if(state.cartList.length > 0){
            state.cartList.forEach((item,index)=>{
                item.status =  status
            })
            localStorage.setItem('cartList', JSON.stringify(state.cartList))
        }
    },
    // 商品的数量
    setGoodsCount(state,obj){
        if(state.cartList.length > 0){
            state.cartList.some((item,index)=>{
                if(item.cartId == obj.id){
                    item.count = obj.count
                    return
                }
            })
            localStorage.setItem('cartList', JSON.stringify(state.cartList))
        }
    },

    // 删除商品
    setDelGoods(state,arr){
        for(let i = 0 ; i < arr.length; i++){
            for(let j = 0 ; j < state.cartList.length ; j++){
              if(arr[i] == state.cartList[j].cartId){
                state.cartList.splice(j,1)
                console.log("删除成功")
              }
            }
        }
        localStorage.setItem('cartList', JSON.stringify(state.cartList))
    },

    addSelectFn(state, item) {
        state.saveList.unshift(item)
    }

}

export default mutations